<?php
/* 
* @Title:  [属性模型]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-04 21:23:12
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-11 23:25:31
* @Copyright:  [hn7m.com]
*/
class AttrModel extends Model{
	public $table="type_attr";

	/**
	 * [$validate 自动验证规则]
	 * @var array
	 */
	public $validate=array(
		array('aname','nonull','属性名称不能为空',2,3),
		array('acontent','nonull','属性值不能为空',2,3)
		);

	/**
	 * [add_attr_data 添加属性名称]
	 */
	public function add_attr_data($tid){
		$aname=Q('post.aname','','htmlspecialchars');
		$attr=Q('post.acontent');
		$preg="/[\s,\r\n]+/is";
		if (!preg_match($preg, $attr)) {
			$this->error='格式不正确,必须以","或者"换行"结束';
			return false;
		}

		$arr=preg_split($preg, $attr);
		//去重
		$arr=array_unique($arr);

		//循环插入
		foreach ($arr as $key => $value) {
			if(empty($value)) continue;
			$data=array('acontent'=>$value,'qm_type_tid'=>$tid,'aname'=>$aname);
			$this->add($data);
		}
		return true;
	}

	/**
	 * [get_data 获得所有的属性]
	 * @return [type] [description]
	 */
	public function get_data(){

		$tid=Q('tid',0,'intval');

		return $this->where(array('qm_type_tid '=>$tid))->order('aid asc')->findAll();
	}
	public function get_alldata($where=NULL){
		return $this->where($where)->findAll();
	}
	/**
	 * [get_one_data 通过aid获得一条属性]
	 * @return [type] [description]
	 */
	public function get_one_data($aid){
		return $this->where(array('aid'=>$aid))->find();
	}
	/**
	 * [add_data 按照属性名添加属性值]
	 */
	public function update_data($aid,$data){
		return $this->where(array('aid'=>$aid))->save($data);
	}
	/**
	 * [get_onedate_value 获得一条数据的字段值]
	 * @param  [type] $gid  [description]
	 * @param  [type] $need [description]
	 * @return [type]       [description]
	 */
	public function get_onedate_value($aid,$need){
		return $this->where(array('aid'=>$aid))->getField($need);
	}
	/**
	 * [del_data 删除数据]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function del_data($aid){
		return $this->del($aid);
	}
}
?>